package com.example.broadcastnotifytest618lw;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.MediaStore;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

public class MyReceiver extends BroadcastReceiver {

    private Context context;
    private String str;
    static NotificationManager manager;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context=context;

        str=intent.getStringExtra("para1");
        Toast.makeText(context, "收到静态广播"+str, Toast.LENGTH_SHORT).show();

        sendNotify();

    }

    private void sendNotify() {

        manager=
                ( NotificationManager)context.getSystemService
                        (Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder=
                new NotificationCompat.Builder(context);

        Intent intent=
                new Intent(context,NotifyActivity.class);
        intent.putExtra("para2",str);
        PendingIntent pendingIntent=PendingIntent.getActivity(context
                , 1
                ,intent
                ,PendingIntent.FLAG_UPDATE_CURRENT);

        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
            builder.setContentTitle("标题")
                    .setContentText("内容"+str)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI)
                    .setChannelId(context.getPackageName());

            NotificationChannel channel=new NotificationChannel(
                    context.getPackageName()
                    ,"会话类型"
                    ,NotificationManager.IMPORTANCE_DEFAULT
            );
            manager.createNotificationChannel(channel);
        }
        manager.notify(1,builder.build());

    }

}
